
package cz.muni.fi.pa165.carpark.servicies;

import cz.muni.fi.pa165.carpark.EmployeePosition;
import cz.muni.fi.pa165.carpark.TO.ServiceCheckTO;
import cz.muni.fi.pa165.carpark.TO.VehicleTO;
import cz.muni.fi.pa165.carpark.VehicleStatus;
import cz.muni.fi.pa165.carpark.dao.ServiceCheckDAO;
import cz.muni.fi.pa165.carpark.dao.VehicleDAO;
import cz.muni.fi.pa165.carpark.entities.ServiceCheck;
import cz.muni.fi.pa165.carpark.entities.Vehicle;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.ConversionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author xmarton1
 */
@Service
public class VehicleServiceImpl implements  VehicleService{
    
    @Autowired
    private VehicleDAO vehicleDAO;
    
    @Autowired
    private ServiceCheckDAO serviceCheckDAO;
    
    @Autowired
    @Qualifier("TOconverter")
    private ConversionService convertor;

    @PreAuthorize("hasRole('vehicle_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public void create(VehicleTO vehicle) {
        Vehicle v = convertor.convert(vehicle, Vehicle.class);
        vehicleDAO.create(v);
        vehicle.setId(v.getId());
    }

    @PreAuthorize("hasRole('vehicle_manager') or hasRole('superadmin') or hasRole('employee_manager')")
    @Override
    @Transactional
    public VehicleTO retrieve(Long id) {
        Vehicle v = vehicleDAO.get(id);
        VehicleTO vehicleTO = convertor.convert(v, VehicleTO.class);
        return vehicleTO;
    }

    @PreAuthorize("hasRole('vehicle_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public void update(VehicleTO vehicle) {
        Vehicle v = convertor.convert(vehicle, Vehicle.class);
        vehicleDAO.update(v);
    }

    @PreAuthorize("hasRole('vehicle_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public void delete(VehicleTO vehicle) {
        Vehicle v = convertor.convert(vehicle, Vehicle.class);
        vehicleDAO.remove(v);
    }
    
    
    @PreAuthorize("hasRole('vehicle_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public List<VehicleTO> getVehiclesByStatus(VehicleStatus status){
        List<Vehicle> vehicles = vehicleDAO.getVehiclesByStatus(status);
        List<VehicleTO> vehicleTOs = new LinkedList<VehicleTO>();
        for(Vehicle v : vehicles)
        {
            vehicleTOs.add(convertor.convert(v, VehicleTO.class));
        }
        return vehicleTOs;
    }

    @PreAuthorize("hasRole('vehicle_manager') or hasRole('superadmin') or hasRole('employee_manager')")
    @Override
    @Transactional
    public List<VehicleTO> retrieveAllVehiclesForPosition(EmployeePosition position) {
        List<Vehicle> vehicles = vehicleDAO.findAllVehiclesForPosition(position);
        List<VehicleTO> vehicleTOs = new LinkedList<VehicleTO>();
        for(Vehicle v : vehicles)
        {
            vehicleTOs.add(convertor.convert(v, VehicleTO.class));
        }
        return vehicleTOs;
    }

    @PreAuthorize("hasRole('vehicle_manager') or hasRole('superadmin') or hasRole('employee_manager')")
    @Override
    @Transactional
    public List<VehicleTO> retrieveAllVehicles() {
        List<Vehicle> vehicles = vehicleDAO.findAllVehicles();
        List<VehicleTO> vehicleTOs = new LinkedList<VehicleTO>();
        for(Vehicle v : vehicles)
        {
            vehicleTOs.add(convertor.convert(v, VehicleTO.class));
        }
        return vehicleTOs;
    }
    
    @PreAuthorize("hasRole('vehicle_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public List<ServiceCheckTO> getActiveServiceChecks(VehicleTO vehicle,boolean active){
        Vehicle v = convertor.convert(vehicle, Vehicle.class);
        List<ServiceCheck> checks = vehicleDAO.getActiveServiceChecks(v, active);
        
        List<ServiceCheckTO> checkTOs = new LinkedList<ServiceCheckTO>();
        for(ServiceCheck s : checks)
        {
            checkTOs.add(convertor.convert(s, ServiceCheckTO.class));
        }
        return checkTOs;
    }

    public void setVehicleDAO(VehicleDAO vehicleDAO) {
        this.vehicleDAO = vehicleDAO;
    }

    public void setConvertor(ConversionService convertor) {
        this.convertor = convertor;
    }

}
